package Set;
//2、需求：用Set集合存储自定义对象并遍历Student(name,age)
//        分析：
//        A:创建学生类
//        B:创建集合对象
//        C:创建学生对象
//        D:把学生对象添加到集合对象中
//        E:遍历集合（使用迭代器和增强for两种遍历方式）
import java.util.*;
class student{
    String name;
    int age;
    student(String name, int age){
       this.name = name;
       this.age = age;
    }
    String getName(){
        return name;
    }
    int getAge(){
        return age;
    }
}
public class Test {
    public static void main(String[] args) {
        Set s = new HashSet();
        student s1 = new student("zhangsan",14);
        student s2 = new student("lisi",15);
        student s3 = new student("wangwu",16);
        s.add(s1);
        s.add(s2);
        s.add(s3);

        System.out.println("迭代器遍历：");
        Iterator it = s.iterator();
        while(it.hasNext()){
            Object obj = it.next();
            student t = (student)obj;
            System.out.println(t.getName()+" "+t.getAge());
        }
        System.out.println("增强for遍历：");
        for(Object obj : s){
            student t = (student)obj;
            System.out.println(t.getName()+" "+t.getAge());
        }
    }
}
